<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Validation
 *
 * @author bonfiglio
 */
class Filter {

    public static function sanitizeStrictString($string) {
        return preg_replace('/\-?\d+/', '', filter_var($string, FILTER_SANITIZE_STRING, array('flags' => FILTER_FLAG_NO_ENCODE_QUOTES)));
    }

    public static function validateStrictNumber($string) {
        $nstring = preg_replace('/\-?\D+/', '', filter_var($string, FILTER_SANITIZE_STRING, array('flags' => FILTER_FLAG_NO_ENCODE_QUOTES)));
        return (strlen($nstring) != strlen($string)) ? '' : $string; //&& (strlen($nstring) == 0)
    }

    public static function validateMobileFrenchPhoneNumber($string) {
        $nstring = self::validateStrictNumber($string);
        if (!preg_match('#^0[6-7][0-9]{8}$#', $nstring)) {
            return '';
        }
        return (strlen($nstring) != 10) ? '' : $string;
    }

    public static function validateLandlineFrenchPhoneNumber($string) {
        $nstring = self::validateStrictNumber($string);
        if (!preg_match('#^0[1-59][0-9]{8}$#', $nstring)) {
            return '';
        }
        return (strlen($nstring) != 10) ? '' : $string;
    }

    public static function validateFrenchPhoneNumber($string) {
        $nstring = self::validateStrictNumber($string);
        if (!preg_match('#^0[1-9][0-9]{8}$#', $nstring)) {
            return '';
        }
        return (strlen($nstring) != 10) ? '' : $string;
    }

    public static function validateItalianPhoneNumber($string) {
        $nstring = self::validateStrictNumber($string);
        if (!preg_match('#^[03][0-9]{9}$#', $nstring)) {
            return '';
        }
        return (strlen($nstring) != 10) ? '' : $string;
    }

    public static function validateFrenchPostalCode($string) {
        $nstring = self::validateStrictNumber($string);
        if (!preg_match('#^[0-9]{5}$#', $nstring)) {
            return '';
        }
        return (strlen($nstring) != 5) ? '' : $string;
    }

    public static function validateYear($string) {
        $nstring = self::validateStrictNumber($string);
        if (!preg_match('#^[0-9]{4}$#', $nstring)) {
            return '';
        }
        return (strlen($nstring) != 4) ? '' : $string;
    }

    public static function stripAccents($string) {
        return strtr($string, 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ', 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
    }
    public static function validateFloat($value){
        $value = trim(str_replace(',','.',$value));
        if($value == 0){
            $value = '0.00';
        }
        return filter_var($value,FILTER_VALIDATE_FLOAT);
    }
    public static function stripInvisibleCharacter($string) {
        if (is_string($string)) {
            $string = str_replace(CHR(13), '', $string); //retour chariot
            $string = str_replace(CHR(10), '', $string); //retour a la ligne
            $string = str_replace(CHR(9), '', $string); //tabulation
            $string = str_replace(CHR(11), '', $string); //tabulation verticale
            $string = preg_replace('/\s+/', ' ', $string);
        }
        return $string;
    }

}

?>
